import {
    EnchantmentTypes,
    EntityEffectOptions,
    ItemComponentTypes,
    ItemStack,
} from "@minecraft/server";
import { EffectIds } from "@sapi-game/utils/vanila-data";
import { HideAndSeekEffectActuator } from "./components/itemEffectActuator";
import { HideAndSeekPlayer, HideAndSeekRole } from "./player";

export interface LuckyItem {
    func?: (p: HideAndSeekPlayer, actuator: HideAndSeekEffectActuator) => void;
    effect?: { id: EffectIds; duration: number } & EntityEffectOptions;
    item?: ItemStack;
    mes?: string;
    weight: number;
}

export const HideAndSeekLuckyItems: LuckyItem[] = [
    {
        weight: 20,
        effect: { id: EffectIds.JumpBoost, duration: 200, amplifier: 1 },
    },
    {
        weight: 20,
        effect: { id: EffectIds.Speed, duration: 200, amplifier: 1 },
    },
    {
        weight: 15,
        effect: { id: EffectIds.Invisibility, duration: 100, amplifier: 1 },
    },
    {
        weight: 10,
        mes: "什么都没有，下次再来吧",
    },
    {
        weight: 5,
        func(p, actuator) {
            if (p.role == HideAndSeekRole.Hider) {
                p.player?.addEffect(EffectIds.Resistance, 200, {
                    amplifier: 10,
                    showParticles: false,
                });
                p.sendMessage("你获得了10s无敌");
            } else {
                actuator.locate(p);
            }
        },
    },
    {
        weight: 4,
        func(p, actuator) {
            if (p.role == HideAndSeekRole.Seeker) {
                actuator.tp(p);
                return;
            }
            //咬人剑
            const InstantKillSword = new ItemStack("netherite_sword");
            const Durability = InstantKillSword.getComponent(
                ItemComponentTypes.Durability
            )!;
            Durability.damage = Durability.maxDurability - 1;
            const enchant = InstantKillSword.getComponent(
                ItemComponentTypes.Enchantable
            )!;

            enchant.addEnchantment({
                type: EnchantmentTypes.get("sharpness")!,
                level: 5,
            });
            enchant.addEnchantment({
                type: EnchantmentTypes.get("fire_aspect")!,
                level: 2,
            });
            InstantKillSword.nameTag = "咬人剑";
            p.giveItem(InstantKillSword);
            p.sendMessage("你获得了一把剑");
        },
    },
];
